'use strict';

const Controller = require('egg').Controller;
/**
* @controller 前台博客接口
*/
class HomeController extends Controller {
  // 测试
  async index() {
    const result = await this.app.mysql.get('blog_content', {});
    console.log(result);
    this.ctx.body = result;
  }

  /**
   * @summary 获取文章列表接口
   * @description 获取文章列表
   * @router get /api/feblog/getArticleList （ get 表示设置请求为 get 请求，最后的 selectById 对应下面的 selectById 方法 ）。
   * @response 200 articleList 返回结果。（ 对应 contract 里面的验证属性，下面会提到 。）
   */

  async getArticleList() {
    const sql = 'SELECT article.id as id,' +
      'article.title as title,' +
      'article.introduce as introduce,' +
      "FROM_UNIXTIME(article.addTime,'%Y-%m-%d %H:%i:%s' ) as addTime," +
      'article.view_count as view_count ,' +
      'type.typeName as typeName ' +
      'FROM article LEFT JOIN type ON article.type_id = type.Id';
    const results = await this.app.mysql.query(sql);
    this.ctx.body = {
      data: results,
    };
  }
  /**
   * @summary  根据Id获取文章详情接口
   * @description 根据Id获取文章详情接口
   * @router get /api/feblog/getArticleById （ get 表示设置请求为 get 请求，最后的 selectById 对应下面的 selectById 方法 ）。
   * @request query integer Id
   * @response 200
   */

  async getArticleById() {
    // 先配置路由的动态传值，然后再接收值
    const id = this.ctx.params.id;
    console.log(id);
    const sql = 'SELECT article.id as id,' +
      'article.title as title,' +
      'article.article_content as article_content,' +
      'article.introduce as introduce,' +
      "FROM_UNIXTIME(article.addTime,'%Y-%m-%d %H:%i:%s' ) as addTime," +
      'article.view_count as view_count ,' +
      'type.typeName as typeName ,' +
      'type.id as typeId ' +
      'FROM article LEFT JOIN type ON article.type_id = type.Id ' +
      'WHERE article.id=' + id;
    const result = await this.app.mysql.query(sql);
    console.log(result);
    this.ctx.body = { data: result };
  }


  /**
   * @summary  获取头部nav的接口
   * @description 获取头部nav的接口
   * @router get /api/feblog/getTypeInfo
   * @response 200
   */
  // 获取头部nav的接口
  async getTypeInfo() {
    const sql = 'select * from blog_nav';
    const res = await this.app.mysql.query(sql);
    this.ctx.body = {
      data: res,
    };
  }
}

module.exports = HomeController;
